Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

CLI: Only read stdin if there were no files supplied and not in tty mode. #567

Closed
wants to merge 1 commit into from

Conversation

mrjoelkemp
Copy link
Member

Fixes #563

@@ -106,7 +106,7 @@ module.exports = function(program) {
checker.registerDefaultRules();
checker.configure(config);

if (! process.stdin.isTTY) {
if (!process.stdin.isTTY && !args.length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if an argument other than filename was specified?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like what? Options don't add data to the args array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. Good.

@markelog
Copy link
Member

$ ./bin/jscs -
Error: Path - was not found.
    at ../jscs/lib/checker.js:111:19
    at Array.0 (../jscs/node_modules/vow-fs/node_modules/vow/lib/vow.js:555:56)
    at Object.callFns [as _onImmediate] (../jscs/node_modules/vow-fs/node_modules/vow/lib/vow.js:1148:35)
    at processImmediate [as _immediateCallback] (timers.js:336:15)

Test for this we would be preferable

@@ -1,7 +1,5 @@
#!/usr/bin/env node
var cli = require('../lib/cli');
var stdin = process.stdin;
var _stdInput = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sad artefact, could you do it in another commit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: #570

@mrjoelkemp
Copy link
Member Author

@markelog Ready for another review.

I stubbed a few of the stdin tests. However, I need more time to think through stubbing of the other stdin tests. That shouldn't hold up this PR and would be more appropriate for #547.


it('should not accept piped input if files were specified (#563)', function() {
var checker = require('../lib/checker');
var spy = sinon.spy(checker.prototype, 'checkPath');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as in test below you need to restore the method you are spying on – see "Spying on existing methods" in here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restoring this method works well. Good catch. Done.

});

it('should check stdin if - was supplied as the last argument (#563)', function() {
var spy = sinon.spy(process.stdin, 'on');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markelog Restoring process.stdin.on is problematic. I'm not 100% sure why (still trying to find the cause through sinon's source), but process.stdin.on.restore only exists until the first call to process.stdin.on at https://github.com/mrjoelkemp/node-jscs/blob/fix_stdin/lib/cli.js#L117. After this call, process.stdin.on.restore is gone. Hence, adding a restore call in the promise callback throws an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, it's already restored, by the time the test is done, so we don't need to do that ourselves

@benesch
Copy link
Contributor

benesch commented Aug 19, 2014

Why not extract https://github.com/mrjoelkemp/node-jscs/blob/3a82d8ee8a3227e7d01a68c30772b180e129ea0b/lib/cli.js#L112-L130 into a checker method checker.checkStdin()?

Then you can just add a spy on checkStdin and avoid process.stdin entirely.

@mrjoelkemp
Copy link
Member Author

@benesch Great tip!

I'm thinking something like checker.checkStdin(callback) where callback resolves with the input string.

I think this improvement could be in a separate PR since it would require a refactor of cli.js and new tests for checker. process.stdin.on no longer being a spy (for some reason) doesn't seem like a blocker since the suite passes.

@markelog
Copy link
Member

Merged

@benesch
Copy link
Contributor

benesch commented Aug 19, 2014

Sweet! Thanks a bunch, guys.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reading from stdin logic still broken
4 participants